package com.diandian;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.util.Scanner;

/**
 * Description of Multi
 * Copyright 2017 Meorient, Inc. All rights reserved.
 * 负责将所有客服端的
 *
 * @author yuancheng.li on 2018年03月22日
 */
public class Multi {

    // 224.0.0.0 239.255.255.255

    public static void main(String[] args) throws IOException {

        new Multi().init();

    }

    public void init() throws IOException {

        String ip = "230.0.0.1";
        int port = 9999;
        MulticastSocket socket = new MulticastSocket(port);
        InetAddress anInterface = InetAddress.getByName(ip);
        socket.joinGroup(anInterface);
        // 不接受自己的消息
        socket.setLoopbackMode(true);
        MultiThread multiThread = new MultiThread(socket, anInterface);
        Thread thread = new Thread(multiThread);
        thread.setDaemon(true);thread.start();
        System.out.println("ready");
        System.out.println("退出请输入: exist");
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {

            String read = scanner.nextLine();
            if (read == null || read.length() == 0 || read.equals("exist")) {
                multiThread.setStop(true);
                System.out.println("5秒钟之后退出");
                try {
                    Thread.sleep(5000L);
                } catch (InterruptedException e) {
                }
                break;
            }

            byte[] bytes = read.getBytes();
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, anInterface, port);
            socket.send(packet);
        }

    }

    public static class MultiThread implements Runnable {

        private static final int LEN = 4096;
        private MulticastSocket socket;
        private byte[] buffer = new byte[LEN];

        private DatagramPacket datagramPacket;

        private InetAddress anInterface;

        private boolean isStop = false;

        public MultiThread(MulticastSocket socket, InetAddress anInterface) {
            this.socket = socket;
            this.anInterface = anInterface;
            datagramPacket = new DatagramPacket(buffer, LEN);
        }

        public boolean isStop() {
            return isStop;
        }

        public void setStop(boolean stop) {
            isStop = stop;
        }

        @Override
        public void run() {

            while (!isStop) {
                try {
                    socket.receive(datagramPacket);
                    byte[] data = datagramPacket.getData();
                    System.out.println("received msg==> " + new String(data, 0, datagramPacket.getLength()));
                } catch (IOException e) {
                    e.printStackTrace();
                    try {
                        socket.leaveGroup(anInterface);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    socket.close();
                }
            }
            System.out.println("线程退出");
        }
    }

}
